#region License
// Copyright (c) 2007 James Newton-King
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
#endregion

using System;
using System.Collections.Generic;
using System.Text;
using Newtonsoft.Utilities.Testing;
using NUnit.Framework;
using Newtonsoft.Utilities.Text;

namespace Newtonsoft.Utilities.Tests.Text
{
  public class StringBufferTests : TestsBase
  {
    [Test]
    public void ConstructorEmpty()
    {
      StringBuffer buffer = new StringBuffer();
      Assert.AreEqual(string.Empty, buffer.ToString());
      Assert.AreEqual(0, buffer.Position);
    }

    [Test]
    public void ConstructorInitialSize()
    {
      StringBuffer buffer = new StringBuffer(10000);
      Assert.AreEqual(string.Empty, buffer.ToString());
      Assert.AreEqual(0, buffer.Position);

      buffer.Append('!');
      Assert.AreEqual("!", buffer.ToString());
      Assert.AreEqual(1, buffer.Position);
    }

    [Test]
    [ExpectedException(typeof(ArgumentOutOfRangeException), ExpectedMessage = @"Argument cannot be negative.
Parameter name: initialSize
Actual value was -80.")]
    public void ConstructorNegativeInitialSize()
    {
      new StringBuffer(-80);
    }

    [Test]
    public void ConstructorInitialBuffer()
    {
      string testString = "This is a test string.";

      StringBuffer buffer = new StringBuffer(testString.ToCharArray());
      Assert.AreEqual(testString, buffer.ToString());
      Assert.AreEqual(testString.Length, buffer.Position);

      buffer.Append('!');
      Assert.AreEqual(testString + "!", buffer.ToString());
      Assert.AreEqual(testString.Length + 1, buffer.Position);
    }

    [Test]
    [ExpectedException(typeof(ArgumentNullException), ExpectedMessage = @"Value cannot be null.
Parameter name: internalBuffer")]
    public void ConstructorInitialBufferNull()
    {
      new StringBuffer(null);
    }

    [Test]
    public void Append()
    {
      StringBuffer buffer = new StringBuffer();
      buffer.Append('0');
      buffer.Append('1');
      buffer.Append('2');
      buffer.Append('3');
      buffer.Append('4');

      Assert.AreEqual("01234", buffer.ToString());
    }

    [Test]
    public void Append_NullValue()
    {
      StringBuffer buffer = new StringBuffer();
      buffer.Append('1');
      buffer.Append(null);
      buffer.Append('2');

      Assert.AreEqual("12", buffer.ToString());
    }

    [Test]
    public void Clear()
    {
      StringBuffer buffer = new StringBuffer("Pie".ToCharArray());
      Assert.AreEqual("Pie", buffer.ToString());
      Assert.AreEqual(3, buffer.Position);

      buffer.Clear();
      Assert.AreEqual(string.Empty, buffer.ToString());
      Assert.AreEqual(0, buffer.Position);
    }

    [Test]
    [ExpectedException(typeof(ArgumentOutOfRangeException), ExpectedMessage = @"Argument cannot be negative.
Parameter name: value
Actual value was -1.")]
    public void PositionNegative()
    {
      StringBuffer buffer = new StringBuffer();
      buffer.Position = -1;
    }

    [Test]
    public void PositionOutsideBounds()
    {
      StringBuffer buffer = new StringBuffer();
      buffer.Position = 1;
      buffer.Append('!');

      Assert.AreEqual("\0!", buffer.ToString());
      Assert.AreEqual(2, buffer.Position);
    }

    [Test]
    public void PositionChangeAppend()
    {
      StringBuffer buffer = new StringBuffer();
      buffer.Append('0');
      buffer.Append('1');
      buffer.Append('2');
      buffer.Append('3');
      buffer.Append('4');

      Assert.AreEqual("01234", buffer.ToString());
      Assert.AreEqual(5, buffer.Position);

      buffer.Position = 3;
      buffer.Append('5');
      buffer.Append('6');

      Assert.AreEqual("01256", buffer.ToString());
      Assert.AreEqual(5, buffer.Position);
    }

    [Test]
    public void AppendArray()
    {
      StringBuffer buffer = new StringBuffer();
      buffer.Append('0');
      buffer.Append('1');
      buffer.Append('2');
      buffer.Append('3');
      buffer.Append('4');

      Assert.AreEqual("01234", buffer.ToString());
      Assert.AreEqual(5, buffer.Position);

      buffer.Append("012345678901234567890123456789".ToCharArray());
      Assert.AreEqual("01234012345678901234567890123456789", buffer.ToString());
      Assert.AreEqual(35, buffer.Position);

      buffer.Append('!');
      Assert.AreEqual("01234012345678901234567890123456789!", buffer.ToString());
      Assert.AreEqual(36, buffer.Position);
    }
  }
}